package gameLogic;

import java.util.ArrayList;
import java.util.Iterator;

public class StrategyForkBlock extends Strategy {
	private int targetStatus; // [observer status] to search for
	private int ownStatus;
	private BoardState opponent;

	public StrategyForkBlock(Board b, BoardState s, Observer ob) {
		super(b, s, ob);

		if (side == BoardState.X) {
			ownStatus = 11;
			targetStatus = 22;
			opponent = BoardState.O;
		} else {
			ownStatus = 22;
			targetStatus = 11;
			opponent = BoardState.X;
		}
	}

	@Override
	public Move[] getAllMove() {
		ArrayList<Move> moveList = new ArrayList<Move>();
		ArrayList<Move> all = getAllAllowed();
		Iterator<Move> i = all.iterator();
		while (i.hasNext()) {
			Move m = new Move(i.next());
			m.v = opponent;
			if (ob.getMoveEffect(m, targetStatus) >= 2) {
				m.v = side;
				// if the move also makes a 2InR, do it
				if (ob.getMoveEffect(m, ownStatus) > 0)
					return new Move[] { m };
				moveList.add(m);
				all.remove(new Move(m.x, m.y, BoardState.EMPTY));
			}
		}
		Move[] ma = moveList.toArray(new Move[0]);
		if (ma == null || ma.length == 0)
			return null;
		else if (ma.length == 1)
			return ma;
		else { // there are more than 1 enemy fork options!!
			System.out.println("AI Found more than 1 Enemy Fork Move !!!!!");
			i = all.iterator();
			while (i.hasNext()) {
				Move m = new Move(i.next());
				m.v = side;
				// make a 2InR to force opponent to defend
				if (ob.getMoveEffect(m, ownStatus) > 0)
					return new Move[] { m };
			}
		}
		System.out.println("WTF, can't seem to prevent enemy fork!");
		return null;
	}

	@Override
	protected int getPriority() {
		// winning has higher priority than blocking
		return 7;
	}

}
